package com.tx.zlm_replay.config;

import com.tx.zlm_replay.common.RException;
import com.tx.zlm_replay.common.RwithD;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author tx
 * @version 1.0
 * @date 2024/3/18 15:23
 * @description: 统一异常拦截
 */
@Slf4j
@RestControllerAdvice
public class ExceptionConfig {
    // 业务异常
    @ExceptionHandler(value = RException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public RwithD rException(RException r){
        log.debug("全局异常拦截:{}",r);
        return r.getErrResult();
    }

    // 其他异常
    @ExceptionHandler(value = Exception.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public RwithD exception(Exception r){
        log.debug("全局异常拦截:{}",r);
        return RwithD.fail(r.toString());
    }
}
